<?php
require_once 'Zend/Session/Namespace.php';
include_once 'sina-weibo/config.php';
include_once 'sina-weibo/weibooauth.php';
class IndexController extends Zend_Controller_Action {
	
	public function init() {
		$this->smarty = Zend_Registry::get ( 'smarty' );
		$this->config = Zend_Registry::get ( 'config' );
		$this->smarty->assign ( 'baseUrl', $this->_request->getBaseUrl ());
	
	}
	
	public function showAction() {
		$o = new WeiboOAuth ( WB_AKEY, WB_SKEY, $_SESSION ['keys'] ['oauth_token'], $_SESSION ['keys'] ['oauth_token_secret'] );
		$last_key = $o->getAccessToken ( $_REQUEST ['oauth_verifier'] );
		$_SESSION ['last_key'] = $last_key;
		echo "授权完成进入你的微博列表页面</a> ";
		
		$this->smarty->assign ( 'title', 'default' );
		$this->smarty->display ( 'index/show.phtml' );
	}
	
	public function indexAction() {
	   	$_SERVER ['SCRIPT_URI'] = "http://localhost/";
	    // $_SERVER ['SCRIPT_URI'] = " http://www.yyms.cn";
	    $o = new WeiboOAuth( WB_AKEY , WB_SKEY  );
        $keys = $o->getRequestToken();
        $aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false , $_SERVER['SCRIPT_URI'].'/index/show');
        $_SESSION['keys'] = $keys;
       //  echo "<a href=\"$aurl\">Use Oauth to login</a>";
		
		$this->smarty->assign ( 'title', 'default' );
		$this->smarty->display ( 'index/index.phtml' );
	}
	public function boboAction() {
		$c = new WeiboClient ( WB_AKEY, WB_SKEY, $_SESSION ['last_key'] ['oauth_token'], $_SESSION ['last_key'] ['oauth_token_secret'] );
	    $ms  = $c->is_followed("刘兴亮");
	    Zend_Debug::dump($ms);
		
		
		
		$this->smarty->assign ( 'title', 'default' );
		$this->smarty->display ( 'index/bobo.phtml' );
	}
	public function toaddweiboAction() {
		$this->smarty->display ( 'index/addWeiBo.phtml' );
	}
	
	public function  addweiboAction(){
		  $config=  Zend_Registry::get("config");
		  $db=Zend_Registry::get("db");
		  if ($this->_request->isPost()){
	        $num=1;
	        $adapter = new Zend_File_Transfer_Adapter_Http();
	        $adapter->setDestination($config->pic->weibo->path);
	        foreach ($adapter->getFileInfo() as $fileInfo){
	       	 $textarea='textarea'.$num;
	       	 $textareaValue=   $this->_request->getParam($textarea);
	         $fileName=$fileInfo['name'] ;
	       	 $exts=@split("[/\\.]",$fileName);
	       	 $extName=$exts[count($exts)-1];
	       	 $reFileName=time().'-'.$num.'.'.$extName;
	       	 $num+=1;
	       	 $adapter->addFilter('Rename',array('source'=>$fileInfo['tmp_name'],'target'=>$reFileName ,'override'=>true));
	       	 if(!$adapter->receive($fileInfo['name'])){
	       	 	$messages= $adapter->getMessages();
	       	 	echo implode("\n",$messages);
	       	 }
	       	$row = array ('imageName'=>$reFileName,'content'=>$textareaValue ,'type'=>'1','state'=>'1','createDate'=>date("Y-m-d h:m:s"));
              // 插入数据的数据表
            $db->insert('weibo_content', $row);

             }
	       }
		 $this->smarty->display ( 'index/addWeiBo.phtml' );
	}
	
     public function  listweiboAction(){
		  
		  $db=Zend_Registry::get("db");
		  //  $sql = $db->quoteInto('select id ,type ,imageName,content from   weibo_content where  state=?', '1');
          // $result = $db->query($sql); 
          $startCount=1;
          $endCount=5;
          $sql ='select id ,type ,imageName,content from   weibo_content limit '.$startCount.' , '.$endCount ;  
          Zend_Debug::dump($sql);
          $result = $db->query($sql); 
          $rows = $result->fetchAll();
          Zend_Debug::dump($rows);
          $this->smarty->assign("rows",$rows);
		  $this->smarty->display ( 'index/addWeiBo.phtml' );
	}
	
	
	
	

}